import React, { useState, useEffect } from 'react';
import styles from './index.less';
import CardCommon from '@/compontents/cardCommon';
import lhTool from 'lh-tool';
import api from '@/http/api';
import TablePage from '@/compontents/tablePage';
import { Button, Modal, Switch } from 'antd';
const { confirm } = Modal;
const { nsBusinessManageList, nsBusinessEdit, nsTypeList } = api;
export default (props: any) => {
  const { schoolId } = JSON.parse(sessionStorage.info).userInfo;
  const col = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      render: (text: any, record: any, index: any) => `${index + 1}`,
    },
    {
      title: '标题',
      dataIndex: 'title',
      width: 200,
      ellipsis: true,
    },
    {
      title: '价格',
      dataIndex: 'price',
      width: 100,
      ellipsis: true,
    },
    {
      title: '总数',
      dataIndex: 'numTotal',
      width: 100,
      ellipsis: true,
    },
    {
      title: '已售',
      dataIndex: 'num',
      width: 100,
      ellipsis: true,
    },
    {
      title: '类型',
      dataIndex: 'nsTypeId',
      width: 200,
      ellipsis: true,
      render: (v1: any) => formatStatus(v1),
    },
    {
      title: '发布时间',
      dataIndex: 'groundTime',
      render: (v1: any) =>
        lhTool.formatDate.format(new Date(v1), 'yyyy/MM/dd hh:mm:ss'),
    },

    {
      title: '操作',
      dataIndex: 'handle',
      width: 200,
      render: (v1: any, v2: any) => (
        <div>
          <Button
            onClick={() => {
              toAdd(0, v2);
            }}
            size="small"
            type="primary"
          >
            编辑
          </Button>
          <Switch
            onChange={e => groundChange(e, v2)}
            style={{ marginLeft: 10 }}
            checked={v2.isGrounding === 1}
            checkedChildren="上架"
            unCheckedChildren="下架"
          />
        </div>
      ),
    },
  ];
  const [list, setList] = useState([]);
  const [nsType, setNsType]: any = useState([]);
  const getList = () => {
    nsBusinessManageList({
      uSchoolId: schoolId,
    }).then(res => {
      setList(res.data);
    });
  };
  const toAdd = (v: any, v2: any) => {
    console.log(v2);
    props.history.push({
      pathname: '/nsBusiness/add',
      state: {
        isAdd: v,
        initData: v ? {} : v2,
      },
    });
  };
  const groundChange = (e: boolean, v: any) => {
    const { id } = v;
    nsBusinessEdit({
      id,
      isGrounding: e ? 1 : 0,
    }).then(res => {
      getList();
    });
  };
  const getTypeList = () => {
    nsTypeList({
      uSchoolId: schoolId,
    }).then(res => {
      setNsType(res.data);
    });
  };

  const formatStatus = (id: any) => {
    if (nsType.length) {
      const name = nsType.filter((item: any) => item.id === id)[0].name;
      return name;
    }
  };

  useEffect(() => {
    getList();
    getTypeList();
  }, []);
  return (
    <CardCommon>
      <div className={styles.wrap}>
        <div className={styles.btnW}>
          <Button
            onClick={() => {
              toAdd(1, {});
            }}
            type="primary"
          >
            新增
          </Button>
        </div>
        {nsType && <TablePage col={col} list={list} />}
      </div>
    </CardCommon>
  );
};
